The operation of the plugin solely depends on the LHS variable which should be the same as that in Open(). The plugin ends up that particular database connection which populated the records into the group variable. Hence each Open() should be followed by a Close().
LHS variable is necessary and compulsory for the proper functioning of all database functions. Hence, the following loop statement will generate $error variable:
while db.RunSQL($ConnStr,$sql)
...........
endwhile